package cz.kfad.ofrss.synchronization;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.preference.PreferenceManager;
import android.util.Log;
import cz.kfad.ofrss.backend.BackendService;
import cz.kfad.ofrss.common.NetworkHelper;

public class SyncService extends Service {

    public static final String TAG = SyncService.class.getSimpleName();

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        final boolean downloadInBackground = PreferenceManager.getDefaultSharedPreferences(this).getBoolean("downloadInBackground", true);

        if (downloadInBackground) {
            Log.i(TAG, "SyncService started");
            if (!NetworkHelper.isNetworkConnected(this)) {
                Log.d(TAG, "No connection for update");
                return Service.START_NOT_STICKY;
            }
            BackendService.createBackend(this).refreshFeeds();
            Log.d(TAG, "Update complete");
        } else {
            Log.i(TAG, "SyncService started, but it doesn't download feeds because of preferences");
        }
        return Service.START_NOT_STICKY;


    }

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

}
